/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.dubbo;

import org.apache.dubbo.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import static yhao.infra.feature.dubbo.ConfigFactory.*;

//@DubboComponentScan(basePackages = "xxx.xxx")
public class DubboConfig {
    @Autowired
    private Environment env;

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig config = new ApplicationConfig();
        config.setOrganization(env.getProperty(DUBBO_APPLICATION_ORG));
        config.setOwner(env.getProperty(DUBBO_APPLICATION_OWNER));
        config.setName(env.getProperty(DUBBO_APPLICATION_NAME));
        config.setLogger("slf4j");
        return config;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig config = new RegistryConfig();
        //使用zookeeper注册中心暴露服务地址
        config.setProtocol("zookeeper");
        config.setAddress(env.getProperty(DUBBO_REGISTRY_ADDRESS));
        config.setTimeout(15000);
        config.setFile(env.getProperty("user.home")
                +"/dubbo-registry/dubbo-registry-"+env.getProperty(LOGGER_APP)+".cache");
        return config;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig config = new ProtocolConfig();
        config.setName("dubbo");
        config.setPort(env.getProperty(DUBBO_PROTOCOL_PORT,Integer.class));
        return config;
    }

    @Bean
    public MonitorConfig monitorConfig() {
        MonitorConfig config = new MonitorConfig();
        //监控中心配置，protocol="registry"，表示从注册中心发现监控中心地址
        config.setProtocol("registry");
        return config;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig config = new ConsumerConfig();
        config.setCheck(false);
        config.setTimeout(15000);
        return config;
    }
}
